package com.ccsoft.zknote.controller;

import com.cc.commontools.entity.DbConfig;
import com.cc.commontools.entity.TableInfo;
import com.cc.commontools.utils.FreemarkerUtils;
import com.cc.commontools.utils.JacksonUtils;
import com.ccsoft.zknote.pojo.system.JsonResult;
import com.ccsoft.zknote.util.CodeGeneratorUtils;
import com.ccsoft.zknote.util.SystemUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/dbManager")
public class DbManagerController {
    @Autowired
    private SystemUtils systemUtils;
    @Autowired
    private CodeGeneratorUtils codeGeneratorUtils;
    @PostMapping("/dbInfoList")
    public JsonResult getDbInfo() throws SQLException, ClassNotFoundException {
        return JsonResult.ok(systemUtils.getDbInfoList());
    }

    @PostMapping("/dbNameList")
    public JsonResult getDbNameList(@RequestBody DbConfig dbConfig) throws SQLException, ClassNotFoundException {
        return JsonResult.ok(systemUtils.getDbNameList(dbConfig));
    }

    @PostMapping("/tableNameList")
    public JsonResult getTableNameList(@RequestBody Map<String, String> params) throws SQLException, ClassNotFoundException {
        String dbName = params.get("dbName");
        return JsonResult.ok(systemUtils.getTableNameList(dbName));
    }

    @PostMapping("/fieldInfoList")
    public JsonResult getFieldInfoList(@RequestBody Map<String,Object> params) throws Exception {
        Map configObj = (Map) params.get("dbConfig");
        DbConfig dbConfig = (DbConfig) JacksonUtils.map2Bean(configObj, DbConfig.class);
        String dbName = (String) params.get("dbName");
        String tableName = (String) params.get("tableName");
        return JsonResult.ok(systemUtils.getFieldInfoList(dbConfig, dbName, tableName));
    }

    @PostMapping("/tableInfoList")
    public JsonResult getTableInfoList8DbName(@RequestBody Map<String,String> params) throws SQLException, ClassNotFoundException {
        String dbName = params.get("dbName");
        return JsonResult.ok(systemUtils.getTableInfoList8DbName(dbName));
    }

    @PostMapping("/tableInfoListNoFields")
    public JsonResult getTableInfoListNoFields(@RequestBody Map<String,Object> params) throws Exception {
        Map configObj = (Map) params.get("dbConfig");
        DbConfig dbConfig = (DbConfig) JacksonUtils.map2Bean(configObj, DbConfig.class);
        String dbName = (String) params.get("dbName");
        return JsonResult.ok(systemUtils.getTableInfoListNoFields(dbConfig, dbName));
    }

    @PostMapping("/tableInfo")
    public JsonResult getTableInfo(@RequestBody Map<String,Object> params) throws SQLException, ClassNotFoundException {
        DbConfig dbConfig = (DbConfig) params.get("dbConfig");
        String dbName = (String) params.get("dbName");
        String tableName = (String) params.get("tableName");
        return JsonResult.ok(systemUtils.getTableInfoAndFields(dbConfig, dbName,tableName));
    }

    @PostMapping("/tableInfoAndFields")
    public JsonResult getTableInfoAndFields(@RequestBody Map<String,Object> params) throws SQLException, ClassNotFoundException {
        DbConfig dbConfig = (DbConfig) params.get("dbConfig");
        String dbName = (String) params.get("dbName");
        String tableName = (String) params.get("tableName");
        return JsonResult.ok(systemUtils.getTableInfoAndFields(dbConfig,dbName,tableName));
    }

    @PostMapping("/generateFile")
    public JsonResult generateFile(@RequestBody FreemarkerUtils.GenerateConfig config) throws Exception {
        List<String> fileNameList = codeGeneratorUtils.generateFile(config);
        return JsonResult.ok(fileNameList);
    }

    @PostMapping("/getTemplateTree")
    public JsonResult getTemplateTree() throws IOException {
        return JsonResult.ok(codeGeneratorUtils.getTemplateTree());
    }
}
